package com.woniuedu.lecturer.service.impl;

import com.aliyun.oss.OSSClient;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuedu.lecturer.bean.UploadResult;
import com.woniuedu.lecturer.dao.CourseMapper;
import com.woniuedu.lecturer.dao.TeacherMapper;
import com.woniuedu.lecturer.dto.FindByIdTeacherDto;
import com.woniuedu.lecturer.dto.RsFindAllTeacherDto;
import com.woniuedu.lecturer.dto.Teacher;
import com.woniuedu.lecturer.entity.Course;
import com.woniuedu.lecturer.entity.ResponseEntity;
import com.woniuedu.lecturer.entity.TeacherQuery;
import com.woniuedu.lecturer.service.TeacherService;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.InputStream;
import java.net.URL;
import java.util.Date;
import java.util.List;
import java.util.Random;

/**
* @author 王成亮
* @description 针对表【edu_teacher(讲师)】的数据库操作Service实现
* @createDate 2022-04-18 17:01:34
*/
@Service
public class TeacherServiceImpl extends ServiceImpl<TeacherMapper, com.woniuedu.lecturer.entity.Teacher>
    implements TeacherService {
    @Resource
    TeacherMapper teacherMapper;

    @Resource
    CourseMapper courseMapper;
    @Resource//注入阿里云客户端
    private OSSClient ossClient;
    //桶名字（阿里云空间名字）
    private static final String BUCKE_NAME = "miku233";
    private static final String URI_REFIX = "https://miku233.oss-cn-chengdu.aliyuncs.com";


    /**
     * Auther: 王成亮
     * Date: 2022/4/19 10:37
     * Description:分页查询所有讲师
     */
    @Override
    public ResponseEntity<RsFindAllTeacherDto> findAll(int page,int limit) {
        //获取分页信息
        Integer currentPage = page;
        Integer pageSize = limit;

        //生成统一返回对象
        ResponseEntity<RsFindAllTeacherDto> responseEntity = new ResponseEntity<>();

        //封装分页信息
        Page<com.woniuedu.lecturer.entity.Teacher> pages = new Page<>(currentPage,pageSize);

        //查询
        List<com.woniuedu.lecturer.entity.Teacher> all = teacherMapper.findAll(pages);
        //返回
        if (all != null) {
            //封装返回dto
            RsFindAllTeacherDto rsFindAllTeacherDto = new RsFindAllTeacherDto();
            rsFindAllTeacherDto.setPage(pages);
            rsFindAllTeacherDto.setTeacherList(all);
            //开始返回
            responseEntity.setCode(200);
            responseEntity.setData(rsFindAllTeacherDto);
            responseEntity.setMsg("查询成功");
        }else {
            responseEntity.setCode(500);
            responseEntity.setMsg("查询失败");
        }
        return responseEntity;
    }
    /**
     * Auther: 王成亮
     * Date: 2022/4/19 10:37
     * Description:删除讲师
     */
    @Override
    public ResponseEntity deleteTeacherById(String id) {
        //生成统一返回对象
        ResponseEntity responseEntity = new ResponseEntity();
        //调用mp自带方法
        int i = teacherMapper.deleteById(id);
        if (i > 0) {
            //开始返回
            responseEntity.setCode(200);
            responseEntity.setMsg("删除成功");
        }else {
            responseEntity.setCode(500);
            responseEntity.setMsg("删除失败");
        }
        return responseEntity;
    }
    /**
     * Auther: 王成亮
     * Date: 2022/4/19 10:37
     * Description:更新讲师
     */
    @Override
    public ResponseEntity updateTeacher(com.woniuedu.lecturer.entity.Teacher teacher) {
        //生成统一返回对象
        ResponseEntity responseEntity = new ResponseEntity<>();
        //调用mp自带方法
        int i = teacherMapper.updateById(teacher);
        if (i > 0) {
            //开始返回
            responseEntity.setCode(200);
            responseEntity.setMsg("更新成功");
        }else {
            responseEntity.setCode(500);
            responseEntity.setMsg("更新失败");
        }
        return responseEntity;
    }
    /**
     * Auther: 王成亮
     * Date: 2022/4/19 10:37
     * Description:上传讲师头像
     */
    @Override
    public ResponseEntity<UploadResult> upload(MultipartFile file,String id) {
        Random rd = new Random();
        //1.获取文件的名字,重命名
        String fileName = id+rd+file.getOriginalFilename();

        //2.获取文件的流对象
        //生成统一返回对象
        ResponseEntity<UploadResult> result = new ResponseEntity<>();
        try {
            InputStream inputStream = file.getInputStream();
            //3.利用阿里云上传文件
            //参数1：桶的名字
            //参数2:文件名
            //参数3:流对象
            ossClient.putObject(BUCKE_NAME,fileName,inputStream);
            //4.设置文件过期时间
            Date date = new Date(System.currentTimeMillis() + 1000*60*60*24*30);
            //5.上传文件、指定过期时间、返回文件的uri
            URL url = ossClient.generatePresignedUrl(BUCKE_NAME, fileName, date);
            System.out.println(url.getPath());

            //封装结果返回类
            UploadResult uploadResult = new UploadResult();
            uploadResult.setFileName(fileName);
            uploadResult.setUrl(URI_REFIX+url.getPath());
            System.out.println(URI_REFIX+url.getPath());

            //调接口更新图片
            com.woniuedu.lecturer.entity.Teacher teacher = new com.woniuedu.lecturer.entity.Teacher();
            teacher.setId(id);
            System.out.println("id:"+teacher.setId(id));
            if (!teacher.getId().equals("上传")) {
                int i = teacherMapper.updateById(teacher);
                if (i > 0) {
                    result.setCode(200);
                    result.setMsg("上传成功");
                    result.setData(uploadResult);
                }else {
                    result.setCode(500);
                    result.setMsg("上传失败");
                }
            }else {
                result.setCode(200);
                result.setMsg("上传成功");
                result.setData(uploadResult);
            }
            return result;
        }catch (Exception e){
            e.printStackTrace();
            result.setCode(500);
            result.setMsg("上传失败");
        }
        return null;
    }

    /**
     * Auther: 王成亮
     * Date: 2022/4/19 10:37
     * Description:新增讲师
     */
    @Override
    public ResponseEntity<Teacher> addTeacher(Teacher addTeacherDto) {
        //生成统一返回对象
        ResponseEntity responseEntity = new ResponseEntity();
        com.woniuedu.lecturer.entity.Teacher teacher = new com.woniuedu.lecturer.entity.Teacher();
        teacher.setName(addTeacherDto.getName());
        teacher.setAvatar(addTeacherDto.getAvatar());
        teacher.setIntro(addTeacherDto.getIntro());
        teacher.setLevel(addTeacherDto.getLevel());
        teacher.setSort(addTeacherDto.getSort());
        teacher.setCareer(addTeacherDto.getCareer());
        System.out.println("teacher"+teacher);
        //调方法
        int insert = teacherMapper.insert(teacher);
        if (insert > 0) {
            //开始返回
            responseEntity.setCode(200);
            responseEntity.setMsg("新增成功");
        }else {
            responseEntity.setCode(500);
            responseEntity.setMsg("新增失败");
        }
        return responseEntity;
    }

    /**
     * Auther: 王成亮
     * Date: 2022/4/19 10:37
     * Description:根据id查询讲师
     */
    @Override
    public ResponseEntity<FindByIdTeacherDto> findTeacherById(String id) {
        //生成统一返回对象
        ResponseEntity<FindByIdTeacherDto> responseEntity = new ResponseEntity<>();
        //生产返回对象
        FindByIdTeacherDto findByIdTeacherDto = new FindByIdTeacherDto();
        //调方法
        com.woniuedu.lecturer.entity.Teacher teacher = teacherMapper.selectById(id);
        List<Course> byTeacherId = courseMapper.findByTeacherId(id);
        if (teacher != null || byTeacherId != null) {
            responseEntity.setCode(200);
            responseEntity.setMsg("查询成功");
            findByIdTeacherDto.setTeacher(teacher);
            findByIdTeacherDto.setCourseList(byTeacherId);
            responseEntity.setData(findByIdTeacherDto);
        }else {
            responseEntity.setCode(500);
            responseEntity.setMsg("查询失败");
        }
        return responseEntity;
    }

    /**
     * Auther: 王成亮
     * Date: 2022/4/19 10:37
     * Description:根据条件分页查询讲师
     */
    @Override
    public ResponseEntity findTeacherByTeacher(int current, int limit, TeacherQuery teacherQuery) {
        //生成统一返回对象
        ResponseEntity responseEntity = new ResponseEntity<>();

        //引入pagehelp,来实现分页功能
        PageHelper.startPage(current,limit);
        //创建
        com.woniuedu.lecturer.entity.Teacher teacher = new com.woniuedu.lecturer.entity.Teacher();

        //封装查询条件
        teacher.setId(teacherQuery.getId());
        teacher.setName(teacherQuery.getName());
        teacher.setLevel(teacherQuery.getLevel());
        List<com.woniuedu.lecturer.entity.Teacher> teacherList = teacherMapper.findAllByIdOrLevelOrNameOrGmtCreateOrGmtModified(teacher);

        PageInfo<com.woniuedu.lecturer.entity.Teacher> pageInfo=PageInfo.of(teacherList);

        if (teacherList != null) {
            responseEntity.setCode(200);
            responseEntity.setMsg("查询成功");
            responseEntity.setData(pageInfo);
        }
        return responseEntity;
    }
}




